﻿using Coldairarrow.Business.PB;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness.DTO;
using Coldairarrow.Util;
using Coldairarrow.Util.ClassLibrary;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PB
{
    [Route("/PB/[controller]/[action]")]
    public class PB_DispatchController : BaseApiController
    {
        #region DI
        private readonly RedisOptions _redisOptions; //1.声明实体

        private readonly MongoDBOptions _mongodbOptions;
        public PB_DispatchController(IPB_DispatchBusiness pB_DispatchBus, IOptions<RedisOptions> redisOptions, IOptions<MongoDBOptions> mongodbOptions)
        {
            _pB_DispatchBus = pB_DispatchBus;
            _redisOptions = redisOptions.Value;//3.赋值
            _mongodbOptions = mongodbOptions.Value;
        }

        IPB_DispatchBusiness _pB_DispatchBus { get; }

        #endregion


        #region 获取

        [HttpPost]
        public async Task<PageResult<PB_Dispatch>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _pB_DispatchBus.GetDataListAsync(input);
        }

        /// <summary>
        /// 获取派工信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_DispatchDTO>> GetDispatchDataList(PageInput<PB_DispatchConditionDTO> input)
        {
            return await _pB_DispatchBus.GetDispatchDataListAsync(input);
        }

        [HttpPost]
        public async Task<PB_Dispatch> GetTheData(IdInputDTO input)
        {
            return await _pB_DispatchBus.GetTheDataAsync(input.id);
        }
        [HttpPost]
        public async Task<List<PB_DispatchDetailDTO>> SelectParint(string ParentId)
        {
            var res = await _pB_DispatchBus.SelectParint(ParentId);
            return res;
        }
        [HttpPost]
        public async Task<List<PB_DispatchDTO>> Selectid(string ID)
        {
            var res = await _pB_DispatchBus.Selectid(ID);
            return res;
        }
        [HttpPost]
        public async Task<List<PB_DispatchDetailDTO>> SelectParints(string parints) {
            var res = await _pB_DispatchBus.SelectParints(parints);
            return res;
        }
        /// <summary>
        /// 派工选择人员信息
        /// </summary>
        /// <param name="dispatchDate">派工日期</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<ScheduleDetailDTO>> GetScheduleDetailUserList(string dispatchDate)
        {
            return await _pB_DispatchBus.GetScheduleDetailUserListAsync(dispatchDate);
        }

        /// <summary>
        /// 判断员工是否有派工
        /// </summary>
        /// <param name="dispatchDate">日期</param>
        /// <param name="userid">员工ID</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Object> GetUserWhetherDispatch(string dispatchDate, string userid)
        {
            return await _pB_DispatchBus.GetUserWhetherDispatchAsync(dispatchDate, userid);
        }

        /// <summary>
        /// 保存生产派工及派工明细
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SavalistData(Entity.DTO.PB_DispatchListDTO data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);
                await _pB_DispatchBus.AddData(data);
            }
        }

        /// <summary>
        /// 根据派工编号查找派工详情
        /// </summary>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_DispatchDTO>> GetDataListInfo(List<string> ParentId) {
            var datainfo = await _pB_DispatchBus.GetDataListInfo(ParentId);
            return datainfo;
        }
        /// <summary>
        /// 获取派工单号
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string GetWorkOrderNo()
        {
            return _pB_DispatchBus.GetWorkOrderNoAsync(_redisOptions);
        }

        /// <summary>
        /// 派工获取工序
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<ProcecureSubTreeDTO>> GetTreeSubList()
        {
            return await _pB_DispatchBus.GetTreeSubListAsyncs();
        }

        /// <summary>
        /// 派工获取项目信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_ProjectDataDTO>> GetProject()
        {
            return await _pB_DispatchBus.GetProjectAsync();
        }

        /// <summary>
        /// 派工获取工件信息
        /// </summary>
        /// <param name="projectId">项目Id</param>
        /// <param name="subId">工序Id</param>
        /// <param name="input">筛选条件</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<WorkpieceDTO>> GetMaterial(string projectId, string subId, PageInput<PBMaterialConditionDTO> input)
        {
            return await _pB_DispatchBus.GetMaterialAsync(projectId, subId, input);
        }

        /// <summary>
        /// 派工打印
        /// </summary>
        /// <param name="qrcode">二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_DispatchDataDTO>> GetDispatchListDataPrint(string qrcode)
        {
            return await _pB_DispatchBus.GetDispatchListDataPrintAsync(qrcode);
        }

        /// <summary>
        /// 获取物料信息
        /// </summary>
        /// <param name="MaterialId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_Material>> GetMaterialData(string MaterialId)
        {
            return await _pB_DispatchBus.GetMaterialDataAsync(MaterialId);
        }

        /// <summary>
        /// 获取派工的任务单
        /// </summary>
        /// <param name="projectId">项目Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_ProductionOrder>> GetProductionOrderListData(string projectId, string POName, DateTime? StateTime, DateTime? EndTime)
        {
            return await _pB_DispatchBus.GetProductionOrderListDataAsync(projectId, POName, StateTime, EndTime);
        }
        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(PB_Dispatch data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _pB_DispatchBus.AddDataAsync(data);
            }
            else
            {
                await _pB_DispatchBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _pB_DispatchBus.DeleteDataAsync(ids);
        }

        /// <summary>
        /// 派工单作废
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task TovoidDispatch(string Id) {
            await _pB_DispatchBus.TovoidDispatch(Id);
        }
        #endregion


        #region PDA 

        /// <summary>
        /// 生产领料查询 
        /// </summary>
        /// <param name="cateqrcode">生产二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_DispatchDetailPDADTO> GetPDADataListInfo(string cateqrcode)
        {
            var res = await _pB_DispatchBus.GetPDADataListInfo(cateqrcode);
            return res;
        }

        /// <summary>
        /// PDA上件派工
        /// </summary>
        /// <param name="QrCode"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_CirculationBillInfo>> GetCirculationBillInf(string QrCode) {
            var res = await _pB_DispatchBus.GetCirculationBillInf(QrCode);
            return res;
        }
        [HttpPost]
        public async Task<PieceDispatch> SavePieceDispatch(PieceDispatch Data) {
            var res = await _pB_DispatchBus.SavePieceDispatch(Data, _redisOptions);
            return res;
        }
        /// <summary>
        /// 获取生产任务单对应的包明细
        /// </summary>
        /// <param name="stockupQrCode">生产任务单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_DispatchLoadListDataDTO>> GetStockUpDetailListData(string stockupQrCode)
        {
            return await _pB_DispatchBus.GetStockUpDetailListDataAsync(stockupQrCode);
        }


        /// <summary>
        /// 生产领料物料查询 (第四步,仓库扫对应的物料信息)
        /// </summary>
        /// <param name="localQrCode">货位</param>
        /// <param name="packqrcode">物料二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_DispatchDetailPDANumberDTO> GetPDADataMaterialListInfo(string localQrCode, string packqrcode)
        {
            var res = await _pB_DispatchBus.GetPDADataMaterialListInfo(localQrCode, packqrcode);
            return res;
        }

        /// <summary>
        /// 判断下架出库的包对应货位的项目是否与派工单对应的项目一致  (第五步,判断物料是否有绑定项目)
        /// </summary>
        /// <param name="packqrcode">包二维码</param>
        /// <param name="cateqrcode">派工单二维码</param>
        /// <param name="localQrCode">货位二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> GetLocationProjectTruOrFalse(string packqrcode, string cateqrcode, string localQrCode)
        {
            return await _pB_DispatchBus.GetLocationProjectTruOrFalseAsync(packqrcode, cateqrcode, localQrCode);
        }

        /// <summary>   
        /// 扫描包出库,并对生产任务单做对应物料数量做修改    (和第五步一起执行, 生成备货日志,和出库日志,并且更改库存量)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdateStockUpData(DispatchDTO data)
        {
            await _pB_DispatchBus.UpdateStockUpDataAsync(data);
        }


        /// <summary>
        /// 删除备货单已出库的明细    (移除扫过的物料)
        /// </summary>
        /// <param name="stockupQrCode">备货单二维码</param>
        /// <param name="packqrocde">包二维码</param>
        /// <param name="Nums">数量</param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteStockUpDetailListDataAsync(string stockupQrCode, string packqrocde, double Nums)
        {
            await _pB_DispatchBus.DeleteStockUpDetailListDataAsync(stockupQrCode, packqrocde, Nums);
        }


        /// <summary>
        /// 下架出库提交
        /// </summary>
        /// <param name="stockupQrCode">备货单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SubmitStockUpData(string stockupQrCode)
        {
            await _pB_DispatchBus.SubmitStockUpDataAsync(stockupQrCode);
        }


        /// <summary>
        /// 取消生产领料单提交
        /// </summary>
        /// <param name="stockupQrCode">领料单二维码</param>
        /// 增加取消生产领料单提交功能   
        /// <returns></returns>
        [HttpPost]
        public async Task CancelStockUp(string stockupQrCode)
        {
            await _pB_DispatchBus.CancelStockUpAsync(stockupQrCode);
        }



        /// <summary>
        /// 托盘领料 
        /// </summary>
        /// <param name="carculqrcode">二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_CirculationBillPDADTO> GetPDACarCulDataListInfo(string carculqrcode)
        {
            var res = await _pB_DispatchBus.GetPDACarCulDataListInfo(carculqrcode);
            return res;
        }


        [HttpPost]
     public async   Task<PB_CirculationBillPDADTO> GetPDACarCulDataListInfoDetail(string carculqrcode){
            var res = await _pB_DispatchBus.GetPDACarCulDataListInfoDetail(carculqrcode);
            return res;
}

        /// <summary>
        /// 确认数量,下托盘
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdateStockUpTrayData(DispatchDTO data)
        {
            await _pB_DispatchBus.UpdateStockUpTrayDataAsync(data);
        }


        /// <summary>
        /// 派工单选择工件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<DispatchMaterialDTO>> GetDispatchMaterialList(PageInput<DispatchMaterialConditionDTO> input)
        {
            return await _pB_DispatchBus.GetDispatchMaterialListAsync(input);
        }

        /// <summary>
        /// 派工单选择隶属成品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<OrderMaterialDTO>> GetOrderMaterialList(PageInput<DispatchMaterialConditionDTO> input)
        {
            return await _pB_DispatchBus.GetOrderMaterialListAsync(input);
        }
        /// <summary>
        /// 验证是否派工
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AjaxResult> SaveVerificationDispatch(VerificationDispatch data) {
          return  await _pB_DispatchBus.SaveVerificationDispatch(data);
        }

        #endregion

        #region  快速派工

        /// <summary>
        /// 快速派工保存
        /// </summary>
        /// <param name="data"></param>
        /// <param name="_redisOptions"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveQuickDispatch(QuickDispatch data)
        {
            await _pB_DispatchBus.SaveQuickDispatchAsync(data, _redisOptions);
        }

        /// <summary>
        /// 快速派工获取任务单明细
        /// </summary>
        /// <param name="OrderId">任务单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<ProductionOrderDetailDTO>> GetProductionOrderDetail(string OrderId)
        {
            return await _pB_DispatchBus.GetProductionOrderDetailAsync(OrderId);
        }

        /// <summary>
        /// 快速派工根据成品获取半成品信息
        /// </summary>
        /// <param name="input">成品Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<OrderMaterialProductDTO>> GetOrderMaterialProduct(PageInput<OrderMaterialProductConditionDTO> input)
        {
            return await _pB_DispatchBus.GetOrderMaterialProductAsync(input);
        }

        /// <summary>
        /// 获取排班班组
        /// </summary>
        /// <param name="WorkShopId">车间Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_ShiftType>> GetShiftTypeList(string WorkShopId)
        {
            return await _pB_DispatchBus.GetShiftTypeListAsync(WorkShopId);
        }

        /// <summary>
        /// 快速派工获取排班明细信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<ScheduleDetailDTO>> GetScheduleDetailAsync(PageInput<ScheduleDetailConditionDTO> input)
        {
            return await _pB_DispatchBus.GetScheduleDetailAsync(input);
        }

        /// <summary>
        /// 获取任务单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<Entity.PB.PB_ProductionOrderDTO>> GetProductionOrder(PageInput<PB_ProductionOrderConditionDTO> input)
        {
            return await _pB_DispatchBus.GetProductionOrderAsync(input);
        }

        #endregion

        #region  原材料领料

        /// <summary>
        /// 原材料领料获取物料信息
        /// </summary>
        /// <param name="MaterialQrCode">物料二维码</param>
        /// <param name="LocaQrCode">货位二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<RawStorMaterialDTO> GetRawMaterialData(string MaterialQrCode, string LocaQrCode)
        {
            return await _pB_DispatchBus.GetRawMaterialDataAsync(MaterialQrCode, LocaQrCode);
        }

        /// <summary>
        /// 原材料领料保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveRawMaterialPicking(RawMaterialDTO data)
        {
            await _pB_DispatchBus.SaveRawMaterialPickingAsync(data);
        }

        /// <summary>
        /// 获取部分流转信息
        /// </summary>
        /// <param name="QrCode"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PartialDispatch>> GetPartialDispatchList(string QrCode) {
            var list = await _pB_DispatchBus.GetPartialDispatchList(QrCode);
            return list;
        }
        #endregion

        #region 流转卡初始化报工数据,反写生产任务单已生产数量

        /// <summary>
        /// 获取流转单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResultData<CirculationOrder>> GetCirculationOrderList(PageInput<CirculationOrderConditionDTO> input)
        {
            var res = await _pB_DispatchBus.GetCirculationOrderList(input);
            return res;
        }

        /// <summary>
        /// 获取人员信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<JobUserDTO>> GetJobUserList(PageInput<ConditionDTO> input)
        {
            return await _pB_DispatchBus.GetJobUserListAsync(input);
        }


        /// <summary>
        /// 保存(反向生成上件派工数据,生成报工数据,反写生产任务单已生产数量)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveCirBillJobData(CirBillJobData data)
        {
            await _pB_DispatchBus.SaveCirBillJobDataAsync(data, _redisOptions);
        }

        #endregion

        #region   组装件领料

        /// <summary>
        /// 组装件获取包信息
        /// </summary>
        /// <param name="PackQrCode">包二维码</param>
        /// <param name="DispatchDetailQrCode">派工单明细二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BomPackDTO> GetBomPackDataAsync(string PackQrCode, string DispatchDetailQrCode)
        {
            return await _pB_DispatchBus.GetBomPackDataAsync(PackQrCode, DispatchDetailQrCode);
        }

        /// <summary>
        /// 组装件获取单个物料信息
        /// </summary>
        /// <param name="PackQrCode">物料二维码</param>
        /// <param name="DispatchDetailQrCode">派工单明细二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BomSingleMaterialDTO> GetBomSingleMaterialData(string PackQrCode, string DispatchDetailQrCode)
        {
            return await _pB_DispatchBus.GetBomSingleMaterialDataAsync(PackQrCode, DispatchDetailQrCode);
        }

        /// <summary>
        /// 获取派工单领取明细
        /// </summary>
        /// <param name="DispatchDetailQrCode">派工单明细二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<BomMaterialReceiveMaterialDTO>> GetBomMaterialReceiveList(string DispatchDetailQrCode)
        {
            return await _pB_DispatchBus.GetBomMaterialReceiveListAsync(DispatchDetailQrCode);
        }

        /// <summary>
        /// 组装件领料保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveBomMaterialDataAsync(BomReceiveData data)
        {
            await _pB_DispatchBus.SaveBomMaterialDataAsync(data);
        }

        /// <summary>
        /// 组装件领料移除
        /// </summary>
        /// <param name="data"></param>
        /// <param name="DispatchDetailQrCode">派工单明细二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteBomMaterialReceive(BomMaterialReceiveMaterialDTO data, string DispatchDetailQrCode)
        {
            await _pB_DispatchBus.DeleteBomMaterialReceiveAsync(data, DispatchDetailQrCode);
        }

        /// <summary>
        /// 组装件领料提交
        /// </summary>
        /// <param name="DispatchDetailQrCode">派工单明细二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SubmitBomMaterialReceive(string DispatchDetailQrCode)
        {
            await _pB_DispatchBus.SubmitBomMaterialReceiveAsync(DispatchDetailQrCode);
        }

        /// <summary>
        /// 组装件报工获取物料信息
        /// </summary>
        /// <param name="DispatchDetailQrCode">派工单明细二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<BomMaterialWorkDTO> GetBomMaterialWorkData(string DispatchDetailQrCode)
        {
            return await _pB_DispatchBus.GetBomMaterialWorkDataAsync(DispatchDetailQrCode);
        }

        /// <summary>
        /// 组装件报工
        /// </summary>
        /// <param name="DispatchDetailQrCode">派工单明细二维码</param>
        /// <param name="PrintId">物料码打印机Id</param>
        /// <param name="StorId">仓库Id</param>
        /// <param name="Nums">报工数量</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveBomMaterialWorkAsync(string DispatchDetailQrCode, string PrintId, string StorId, double? Nums)
        {
            await _pB_DispatchBus.SaveBomMaterialWorkAsync(DispatchDetailQrCode, PrintId, StorId, Nums, _mongodbOptions, _redisOptions);
        }

        /// <summary>
        /// 组装报工选择物料打印机
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PrintBomMaterialDTO>> GetPrintBomMaterial()
        {
            return await _pB_DispatchBus.GetPrintBomMaterialAsync();
        }

        /// <summary>
        /// 组装报工选择包码打印机
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PrintBomMaterialDTO>> GetPrintBomPack()
        {
            return await _pB_DispatchBus.GetPrintBomPackAsync();
        }

        #endregion


       
        #region 报废生成新的派工单
       [HttpPost]
        public async Task AddScrappingProducts(ScrappingProducts data) {
            await _pB_DispatchBus.AddScrappingProducts(data);
        }

        [HttpPost]
        public async Task AddProductsDetailReform(ProductsDetailReform data)
        {
            await _pB_DispatchBus.AddProductsDetailReform(data);
        }
        #endregion

    }
}